package cn.itcast.web.action;

import cn.itcast.domain.delivery.WayBill;
import cn.itcast.service.WayBillService;
import cn.itcast.utils.FileUtils;
import cn.itcast.web.action.common.BaseAction;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
//报表 Excel PDF
public class ReportAction extends BaseAction<WayBill> {
    @Autowired
    private WayBillService wayBillService;

    //   action", "../../report_exportXls.action")
    @Action(value = "report_exportXls")
    public String exprotExl() throws IOException {
        //查询全部数据
        List<WayBill> wayBills = wayBillService.findAll();
        //生成Excel文件 sheet->row->cell开始创建
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet hssfSheet = xssfWorkbook.createSheet("一键全部数据报表");
        //表头
        XSSFRow row = hssfSheet.createRow(0);
        row.createCell(0).setCellValue("运单号");
        row.createCell(1).setCellValue("寄件人");
        row.createCell(2).setCellValue("寄件人电话");
        row.createCell(3).setCellValue("寄件人地址");
        row.createCell(4).setCellValue("收件人");
        row.createCell(5).setCellValue("收件人电话");
        row.createCell(6).setCellValue("收件人地址");
        for (WayBill wayBill : wayBills) {
            XSSFRow nextrow = hssfSheet.createRow(hssfSheet.getLastRowNum() + 1);
            nextrow.createCell(0).setCellValue(wayBill.getWayBillNum());
            nextrow.createCell(1).setCellValue(wayBill.getSendName());
            nextrow.createCell(2).setCellValue(wayBill.getSendMobile());
            nextrow.createCell(3).setCellValue(wayBill.getSendAddress());
            nextrow.createCell(4).setCellValue(wayBill.getRecName());
            nextrow.createCell(5).setCellValue(wayBill.getRecMobile());
            nextrow.createCell(6).setCellValue(wayBill.getRecAddress());
        }
           //下载导出
         //设置头信息
        ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
        String filename = "运单数据.xls";
        String agent = ServletActionContext.getRequest().getHeader("user-agent");
        filename = FileUtils.encodeDownloadFilename(filename, agent);
        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename);

        ServletOutputStream outputStream = ServletActionContext.getResponse()
                .getOutputStream();
        xssfWorkbook.write(outputStream);

        // 关闭
        xssfWorkbook.close();
        return NONE;
    }
    //pdf 报表
    //report_exportPdf
    @Action(value = "report_exportPdf")
    public String exprotPdf() throws Exception {
        //查询出需要导出的数据
       /* List<WayBill> wayBills = wayBillService.findAll();
        //下载导出 设置头信息
        ServletActionContext.getResponse().setContentType("application/pdf");
        String filename="运单数据.pdf";
        String agent = ServletActionContext.getRequest().getHeader("user-agent");
        filename = FileUtils.encodeDownloadFilename(filename, agent);
        ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;filename="+filename);
        //生成pdf文件
        Document document = new Document();
        PdfWriter.getInstance(document,ServletActionContext.getResponse().getOutputStream());
        document.open();
        //写pdf数据
        //生成pdf表头
*/

        return NONE;
    }
}
